package com.car.action.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.car.action.util.CommonController;
import com.car.common.model.Card;
import com.car.common.model.CardDiscuss;
import com.car.common.model.CardSee;
import com.car.common.model.UserBlack;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.ehcache.CacheKit;
import com.utils.APIJSON;
import com.utils.StringUtil;
import com.utils.SystemClock;

public class CardNewController  extends CommonController {

	/**
	 * 推荐帖子 
	 */
	public void sel_time() {
		Long user_id = super.getUserId();
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        }
        
        String cache_key = "card__sel_time:"+ user_id + "__" + page;
        ArrayList<Map<String,Object>> result = CacheKit.get("api", cache_key);
        
        if (result == null) {
        	result = Card.dao.sel_time(user_id , page);
        	CacheKit.put("api", cache_key, result);
        }
        renderJson(APIJSON.sendJSON(result));
	}
	
	
	 /**
     *  帖子详情页面
     *  @param card_id
     */
	public void card_detail() {
		Long user_id = super.getUserId();
		Map<String,Object> result = Card.dao.sel_one(getPara("card_id"), user_id);
		if (user_id.intValue() > 0) {
			CardSee cardsee = new CardSee();
			cardsee.setCardId(getParaToInt("card_id"));
			cardsee.setUserId(user_id.intValue());
			cardsee.setCreateTime(SystemClock.now()/1000);
			cardsee.setUpdateTime(SystemClock.now()/1000);
			cardsee.save();
		}
		UserBlack black = UserBlack.dao.findFirst("select id from user_black where status = 1 and user_id = " + user_id +" and black_user_id = " + result.get("user_id") );
		if (black != null) {
			renderText(APIJSON.result(1085));
			return;
		}
		renderJson(APIJSON.sendJSON(result));
		/*
		String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			Long user_id = super.getUserId();
			Map<String,Object> result = Card.dao.sel_one(getPara("card_id"), user_id);
			renderJson(APIJSON.sendJSON(result));
		}
		*/
	}
	
	/**
     * 获取评论列表
     *  @param card_id
     */
	public void discuss_list() {
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        }
        List<Map<String,Object>> result = CardDiscuss.dao.sel_list(getPara("card_id"), page);
        renderJson(APIJSON.sendJSON(result));
        /*
		String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			int page = 1;
	        if(!StringUtil.empty(getPara("page"))) {
	        	page = getParaToInt("page");
	        }
	        List<Map<String,Object>> result = CardDiscuss.dao.sel_list(getPara("card_id"), page);
	        renderJson(APIJSON.sendJSON(result));
		}
		*/
	}
	
}
